#include "StartLayer.h"
#include "PlayLayer.h"
#include "AudioManage.h"

const static char* CONST_PNG_BACKGROUNG = "Start/start_background.png";
const static char* CONST_PNG_BTN_START_N = "Start/btn_start_n.png";
const static char* CONST_PNG_BTN_START_S = "Start/btn_start_s.png";
const static char* CONST_PNG_BTN_HELP_N = "Start/btn_help_n.png";
const static char* CONST_PNG_BTN_HELP_S = "Start/btn_help_s.png";
const static char* CONST_PNG_BTN_EXIT_N = "Start/btn_exit_n.png";
const static char* CONST_PNG_BTN_EXIT_S = "Start/btn_exit_s.png";
static const char* CONST_PNG_CUP_RED = "Cups/cup_red.png";
static const char* CONST_PNG_CUP_YELLOW = "Cups/cup_yellow.png";
static const char* CONST_PNG_CUP_BLUE = "Cups/cup_blue.png";
static const char* CONST_PNG_CUP_GREEN = "Cups/cup_green.png";
static const char* CONST_PNG_CUP_BLACK = "Cups/cup_black.png";
static const char* CONST_PNG_BING = "Start/amigo.png";
static const char* CONST_PNG_ABOUT_N = "Start/btn_about_N.png";
static const char* CONST_PNG_ABOUT_S = "Start/btn_about_S.png";

StartLayer::StartLayer()
{
}


StartLayer::~StartLayer()
{
}

Scene* StartLayer::createScene()
{
	Scene* startScene = Scene::create();
	Layer* startLayer = StartLayer::create();

	startScene->addChild(startLayer);

	return startScene;
}


bool StartLayer::init()
{
	if (!Layer::init())
	{
		return false;
	}

	Size visibleSize = Director::getInstance()->getVisibleSize();

	Sprite* backSprite = Sprite::create(CONST_PNG_BACKGROUNG);
	backSprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
	this->addChild(backSprite);
	

	startBtn = MenuItemImage::create(CONST_PNG_BTN_START_N, CONST_PNG_BTN_START_S,
		CC_CALLBACK_0(StartLayer::startCallBack, this));
	startBtn->setPosition(Vec2(visibleSize.width / 2 , visibleSize.height * 0.35f));

	helpBtn = MenuItemImage::create(CONST_PNG_BTN_HELP_N, CONST_PNG_BTN_HELP_S,
		CC_CALLBACK_0(StartLayer::helpCallBack, this));
	helpBtn->setPosition(Vec2(visibleSize.width / 2, visibleSize.height * 0.25f));

	exitBtn = MenuItemImage::create(CONST_PNG_BTN_EXIT_N, CONST_PNG_BTN_EXIT_S,
		CC_CALLBACK_0(StartLayer::exitCallBack, this));
	exitBtn->setPosition(Vec2(visibleSize.width / 2, visibleSize.height * 0.15f));

	aboutBtn = MenuItemImage::create(CONST_PNG_ABOUT_N, CONST_PNG_ABOUT_S,
		CC_CALLBACK_0(StartLayer::aboutCallBack, this));
	aboutBtn->setPosition(Vec2(visibleSize.width * 0.85, visibleSize.height * 1.2f));

	Menu* startMenu = Menu::create(startBtn, helpBtn, exitBtn, aboutBtn, NULL);
	startMenu->setPosition(Vec2::ZERO);

	this->addChild(startMenu, 10);

	AudioManage::playStartMusic();

	return true;
}

void StartLayer::startCallBack()
{
	AudioManage::stopStartMusic();
	AudioManage::buttonSound();

	auto scene = PlayLayer::createScene();
	Director::getInstance()->replaceScene(scene);
}

void StartLayer::helpCallBack()
{
	AudioManage::buttonSound();

	if (_helpLayer == nullptr)
	{
		_helpLayer = HelpLayer::create();
		this->addChild(_helpLayer, 10);
	}
	else
	{
		HelpLayer* helpLayer = (HelpLayer*)_helpLayer;
		helpLayer->backToFirstPage();
		_helpLayer->setVisible(true);
	}
}

void StartLayer::exitCallBack()
{
	Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

void StartLayer::aboutCallBack()
{
	if (_aboutLayer == nullptr)
	{
		_aboutLayer = AboutLayer::create();
		this->addChild(_aboutLayer, 10);
	}
	else
	{
		_aboutLayer->showLayer();
	}
}


void StartLayer::onEnter()
{
	Layer::onEnter();

	Sprite* redCup = Sprite::create(CONST_PNG_CUP_RED);
	Sprite* yellowCup = Sprite::create(CONST_PNG_CUP_YELLOW);
	Sprite* blueCup = Sprite::create(CONST_PNG_CUP_BLUE);
	Sprite* greenCup = Sprite::create(CONST_PNG_CUP_GREEN);
	Sprite* blackCup = Sprite::create(CONST_PNG_CUP_BLACK);

	cups[0] = blackCup;
	cups[1] = greenCup;
	cups[2] = redCup;
	cups[3] = blueCup;
	cups[4] = yellowCup;

	Size visibleSize = Director::getInstance()->getVisibleSize();

	aboutBtn->runAction(MoveTo::create(1.2f, Vec2(visibleSize.width * 0.85f , visibleSize.height * 0.95f)));

	for (int i = 0; i < 5; i++)
	{
		cups[i]->setPosition(Vec2(visibleSize.width / 2, visibleSize.height * 0.7));
		cups[i]->setRotation(8.0f * (i + 1));
		cups[i]->setVisible(false);
		cups[i]->setScale(0.7f);
		this->addChild(cups[i]);
	}

	float _x = 0.05f;

	cupPositions[0] = Vec2(visibleSize.width * (0.1f + _x), visibleSize.height * 0.3f);
	cupPositions[1] = Vec2(visibleSize.width * (0.12f + _x), visibleSize.height * 0.37f);
	cupPositions[2] = Vec2(visibleSize.width * (0.16f + _x), visibleSize.height * 0.44f);
	cupPositions[3] = Vec2(visibleSize.width * (0.22f + _x), visibleSize.height * 0.51f);
	cupPositions[4] = Vec2(visibleSize.width * (0.31f + _x), visibleSize.height * 0.58f);

	moveCup(0);
}


void StartLayer::moveCup(int cupIndex)
{
	Size visibleSize = Director::getInstance()->getVisibleSize();

	if (cupIndex == 5)
	{
		Sprite* bing = Sprite::create(CONST_PNG_BING);
		bing->setPosition(Vec2(visibleSize.width * 0.8f, visibleSize.height * 0.1f));
		bing->setScale(0.4f);
		this->addChild(bing);
		AudioManage::bellSound();

		return;
	}

	ccBezierConfig bezier;
	bezier.controlPoint_1 = Vec2(visibleSize.width / 2, visibleSize.height / 2);
	bezier.controlPoint_2 = Vec2(visibleSize.width * 0.3f, visibleSize.height * 0.4f);
	bezier.endPosition = cupPositions[cupIndex];

	cups[cupIndex]->runAction(Sequence::create(
		Show::create(),
		BezierTo::create(0.5f, bezier),
		CallFunc::create(CC_CALLBACK_0(StartLayer::moveCup, this, cupIndex + 1)),
		CallFunc::create([]{AudioManage::cupSound(); }),
		NULL
		));

}
